<?php
//////// RichEditor  //////
function BBCodeToHTML($text){

	$patterns[] = "#&#";
	$replacements[] = '&amp;';

	$patterns[] = "#<#";
	$replacements[] = '&lt;';

	$patterns[] = "#>#";
	$replacements[] = '&gt;';

	$patterns[] = "#  #si";
	$replacements[] = '&nbsp;&nbsp;';

	$patterns[] = "#\t#si";
	$replacements[] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';

	$patterns[] = "#\r\n#si";
	$replacements[] = "<br />";

	$patterns[] = "#\n#si";
	$replacements[] = "<br />";

	$patterns[] = "#\[hr\]#si";
	$replacements[] = "<hr class=HR_Color />";	

	//Support Table here
	$patterns[] = "#\[table\]#si";
	$replacements[] = '<table align=center style="border-collapse: collapse;border-spacing: 0px;border: 1px solid #6CAFF7;background-color: #F4F4F4;width:98%;font-family:Verdana,Arial,Sans-Serif,Tahoma;font-size:12px;color: black;">';
	$patterns[] = "#\[\/table\]#si";
	$replacements[] = '</table>';
	$patterns[] = "#\[td\]#si";
	$replacements[] = '<td style="height:25px; border: 1px solid #6CAFF7">';
	$patterns[] = "#\[\/td\]#si";
	$replacements[] = '</td>';
	$patterns[] = "#\[tr\]#si";
	$replacements[] = '<tr>';
	$patterns[] = "#\[\/tr\]#si";
	$replacements[] = '</tr>';

	$patterns[] = "#\[(indent|blockquote)\]#si";
	$replacements[] = "<blockquote>";

	$patterns[] = "#\[\/(indent|blockquote)\]#si";
	$replacements[] = "</blockquote>";

	$patterns[] = "#\[(\/|)sub\]#si";
	$replacements[] = "<$1sub>";

	$patterns[] = "#\[(\/|)sup\]#si";
	$replacements[] = "<$1sup>";

	$patterns[] = "#\[(\/|)strike\]#si";
	$replacements[] = "<$1strike>";

	$patterns[] = "#\[(\/|)u\]#si";
	$replacements[] = "<$1u>";

	$patterns[] = "#\[(\/|)b\]#si";
	$replacements[] = "<$1strong>";

	$patterns[] = "#\[(\/|)i\]#si";
	$replacements[] = "<$1em>";

	$patterns[] = "#\[size=1\]#si";
	$replacements[] = '<span style="font-size: 8pt">';
	$patterns[] = "#\[size=2\]#si";
	$replacements[] = '<span style="font-size: 10pt">';
	$patterns[] = "#\[size=3\]#si";
	$replacements[] = '<span style="font-size: 12pt">';
	$patterns[] = "#\[size=4\]#si";
	$replacements[] = '<span style="font-size: 14pt">';
	$patterns[] = "#\[size=5\]#si";
	$replacements[] = '<span style="font-size: 18pt">';
	$patterns[] = "#\[size=6\]#si";
	$replacements[] = '<span style="font-size: 24pt">';
	$patterns[] = "#\[size=7\]#si";
	$replacements[] = '<span style="font-size: 36pt">';

	$patterns[] = "#\[font=(.*?)\]#si";
	$replacements[] = '<span style="font-family: $1">';

	$patterns[] = "#\[color=(.*?)\]#si";
	$replacements[] = '<span style="color: $1">';

	$patterns[] = "#\[highlight=(.*?)\]#si";
	$replacements[] = '<span style="background-color: $1">';

	$patterns[] = "#\[\/(font|color|size|highlight)\]#si";
	$replacements[] = '</span>';

	$patterns[] = "#\[(center|left|right|justify)\]#si";
	$replacements[] = "<div align=\"$1\">";

	$patterns[] = "#\[\/(center|left|right|justify)\]#si";
	$replacements[] = "</div>";

	$patterns[] = "#\[email=(.*?)\]#si";
	$replacements[] = '<a href="mailto:$1">';
	$patterns[] = "#\[email\](.*?)\[\/email\]#si";
	$replacements[] = '<a href="mailto:$1">$1[/email]';

	$patterns[] = "#\[url=(.*?)\]#si";
	$replacements[] = '<a href="$1">';
	$patterns[] = "#\[url\](.*?)\[\/url\]#si";
	$replacements[] = '<a href="$1">$1[/url]';

	$patterns[] = "#\[\/(email|url)\]#si";
	$replacements[] = "</a>";

	$patterns[] = "#\[img\](.*?)\[\/img\]#si";
	$replacements[] = '<img src="$1" alt="" />';

	$patterns[] = "#\[list=1\]#si";
	$replacements[] = "<ol>";

	$patterns[] = "#\[list\]#si";
	$replacements[] = "<ul>";

	$patterns[] = "#\[\*\]#si";
	$replacements[] = "<li>";

	$patterns[] = "#<br[^>]*><li>#si";
	$replacements[] = "<li>";

	$patterns[] = "#<br[^>]*> <li>#si";
	$replacements[] = "<li>";

	$patterns[] = "#<br[^>]*><\/li>#si";
	$replacements[] = "</li>";

	$patterns[] = "#\[\/list\]#si";
	$replacements[] = '</list>';

	$patterns[] = "#\[FLASH=(.*?),(.*?)\](.*?)\[\/FLASH\]#si";
	$replacements[] = '<object width="$1" height="$2"><param name="movie" value="$3"></param><param name="wmode" value="transparent"></param><embed src="$3" type="application/x-shockwave-flash" wmode="transparent" width="$1" height="$2"></embed></object>';

	$text = preg_replace($patterns, $replacements, $text);
	
	if (preg_match("/<ol/si",$text) || preg_match("/<ul/si",$text)){
		$array=split("<",$text);
		$output="";
		$x=0;
		foreach($array as $line){
			if($x>0)$line="<".$line;
			if(preg_match("/<ol/i",$line)){
				$temp="</ol>";
			}
			elseif(preg_match("/<ul/i",$line)){
				$temp="</ul>";
			}
			if(preg_match("/<\/list>/i",$line)){
				$line=str_replace("</list>",$temp,$line);
			}
			$output.=$line;
			$x++;
		}
	}
	else{
		$output=$text;
	}

	//Try to close tag <li>
	$output=str_replace("<li>","</li><li>",$output);
	$output=str_replace("<ul></li>","<ul>",$output);
	$output=str_replace("<ol></li>","<ol>",$output);
	$output=str_replace("</ul>","</li></ul>",$output);
	$output=str_replace("</ol>","</li></ol>",$output);

	return $output;
}
////// Hien thi loi
////// @param: String error msg
function msgBox($msg){
		
		echo "<script> var str = '$msg'; str = str.toUpperCase(); alert( (str)) </script>";
	}
?>